package com.base.common.web;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

import com.base.common.config.Global;
import com.fasterxml.jackson.annotation.JsonIgnore;

/**
 * 分页类
 */
public class Page<T> {
	
	private int pageNo = 1; // 当前页码
	private int first;// 首页索引
	private int last;// 尾页索引
	private int prev;// 上一页索引
	private int next;// 下一页索引

	private int pageSize = Integer.valueOf(Global.getConfig("page.pageSize")); // 页面大小，设置为“-1”表示不进行分页（分页无效）
	
	private boolean firstPage;//是否是第一页
	private boolean lastPage;//是否是最后一页
	
	private String orderBy = ""; // 标准查询有效， 实例： updatedate desc, name asc
	
	private long fromRowNo;
	private long toRowNo;
	
	@JsonIgnore	
	public int getPageNo() {
		return pageNo;
	}

	public void setPageNo(int pageNo) {
		this.pageNo = pageNo;
	}


	@JsonIgnore
	public int getPageSize() {
		return pageSize;
	}


	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}


	@JsonIgnore
	public long getFromRowNo() {
		if (this.page == 1) {
			this.fromRowNo = 1;
		} else {
			this.fromRowNo = (this.page - 1) * this.pageSize + 1;
		}
		return this.fromRowNo;
	}


	@JsonIgnore
	public long getToRowNo() {
		this.toRowNo = this.page * this.pageSize;
		return this.toRowNo;
	}
	
	/**
	 * 是否为最后一页
	 * @return
	 */
	@JsonIgnore
	public boolean isLastPage() {
		return lastPage;
	}
	
	/**
	 * 下一页索引值
	 * @return
	 */
	@JsonIgnore
	public int getNext() {
		if (isLastPage()) {
			return pageNo;
		} else {
			return pageNo + 1;
		}
	}
	/**
	 * 上一页索引值
	 * @return
	 */
	@JsonIgnore
	public int getPrev() {
		if (isFirstPage()) {
			return pageNo;
		} else {
			return pageNo - 1;
		}
	}
	
	/**
	 * 是否为第一页
	 * @return
	 */
	@JsonIgnore
	public boolean isFirstPage() {
		return firstPage;
	}


	/**
	 * 获取查询排序字符串
	 * @return
	 */
	@JsonIgnore
	public String getOrderBy() {
		// SQL过滤，防止注入 
		String reg = "(?:')|(?:--)|(/\\*(?:.|[\\n\\r])*?\\*/)|"
					+ "(\\b(select|update|and|or|delete|insert|trancate|char|into|substr|ascii|declare|exec|count|master|into|drop|execute)\\b)";
		Pattern sqlPattern = Pattern.compile(reg, Pattern.CASE_INSENSITIVE);
		if (sqlPattern.matcher(orderBy).find()) {
			return "";
		}
		return orderBy;
	}

	/**
	 * 设置查询排序，标准查询有效， 实例： updatedate desc, name asc
	 */
	public void setOrderBy(String orderBy) {
		this.orderBy = orderBy;
	}
	
	
	/**
	 * jqgrid
	 */
	private int page = 1; // 当前页码	
	private long total;
	private long records;
	private List<T> rows = new ArrayList<T>();
	
	
	public int getPage() {
		return page;
	}


	public void setPage(int page) {
		this.page = page;
	}


	public long getRecords() {
		return records;
	}


	public void setRecords(long records) {
		this.records = records;
		
		if (this.records < this.pageSize ) {
			this.total = 1;
		} else if (this.records % this.pageSize > 0) {
			this.total = this.records/this.pageSize + 1;
		} else if (this.records % this.pageSize == 0){
			this.total = this.records/this.pageSize;
		}
	}


	public List<T> getRows() {
		return rows;
	}


	public void setRows(List<T> rows) {
		this.rows = rows;
	}


	public long getTotal() {
		return total;
	}

	
}
